package com.jinzhi.jzweb.controller.moduleConfig;



import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.pojo.ModuleConfig.JzJinzhuanDispositionModule;
import com.jinzhi.jzweb.pojo.ModuleConfig.JzJinzhuanModuleDetail;
import com.jinzhi.jzweb.pojo.ModuleConfig.ModuleTopicMarkScoreVo;
import com.jinzhi.jzweb.pojo.ModuleConfig.ModuleTopicMarkVo;
import com.jinzhi.jzweb.service.moduleConfig.IJzJinzhuanDispositionModuleService;
import com.jinzhi.jzweb.service.moduleConfig.JzJinzhuanModuleDetailService;
import io.swagger.annotations.*;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author JingChangTian
 * @since 2023-06-16
 */
@Controller
@RequestMapping("/jz-jinzhuan-disposition-module")
@Api(value = "模块管理", tags = "模块管理")
public class JzJinzhuanDispositionModuleController  extends BaseController{

    /**
     * 业务层，调用重写mybatisPuls，或其他业务方法
     */
    private final IJzJinzhuanDispositionModuleService dispositionModuleService;

    public JzJinzhuanDispositionModuleController(IJzJinzhuanDispositionModuleService dispositionModuleService) {
        this.dispositionModuleService = dispositionModuleService;
    }



    @ApiOperation(value = "模块页面入口", notes = "模块页面入口")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/getModuleEntrance")
    @RequiresPermissions("jzweb:disposition:module")
    public String moduleEntrance(){
        return "jzweb/moduleEntrance/getModuleEntrance";
    }

    @ApiOperation(value = "新增模块信息", notes = "新增模块信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanDispositionModule", value = "JzJinzhuanDispositionModule", required = true, dataType = "JzJinzhuanDispositionModule"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/insert")
    public Result<String> insert(@RequestBody JzJinzhuanDispositionModule jzJinzhuanDispositionModule){
        //将添加人id存入数据库
        jzJinzhuanDispositionModule.setCreaterId(this.getUserId());
        Boolean aBoolean = dispositionModuleService.insertModule(jzJinzhuanDispositionModule);
        return aBoolean ? Result.ok() : Result.fail();
    }


    @ApiOperation(value = "模块新增页面入口", notes = "模块新增页面入口")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/insertModule")
    @RequiresPermissions("jzweb:disposition:insertModule")
    public String insertModule(){
        return "jzweb/moduleEntrance/insertModule";
    }


    @ApiOperation(value = "模块修改页面入口", notes = "模块修改页面入口")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/updateModule/{id}")
    @RequiresPermissions("jzweb:disposition:updateModule")
    public String updateModule(@PathVariable("id") Long id, Model model){
        model.addAttribute("module",dispositionModuleService.getModuleList(new JzJinzhuanDispositionModule().setId(id)).get(0));
        return "jzweb/moduleEntrance/updateModule";
    }



    @ApiOperation(value = "模块修改数据回显", notes = "模块修改数据回显")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @PostMapping(value = "/updateModuleDate")
    @ResponseBody
    public Result<?> updateModuleDate(@RequestBody JzJinzhuanDispositionModule jzJinzhuanDispositionModule){
        return Result.ok(dispositionModuleService.getModuleList(new JzJinzhuanDispositionModule().setId(jzJinzhuanDispositionModule.getId())));
    }



    @ApiOperation(value = "修改模块信息", notes = "修改模块信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanDispositionModule", value = "JzJinzhuanDispositionModule", required = true, dataType = "JzJinzhuanDispositionModule"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/updateModuleById", method = RequestMethod.POST)
    public Result<String> updateModuleById(@RequestBody JzJinzhuanDispositionModule jzJinzhuanDispositionModule){
        Boolean aBoolean = dispositionModuleService.updateModuleById(jzJinzhuanDispositionModule);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除模块以及下属任务信息", notes = "批量删除模块以及下属任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "模块id集合", value = "ids", required = true, dataType = "List<Long>"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/deleteModuleById", method = RequestMethod.POST)
    public Result<String> deleteModuleById(@RequestParam("ids[]") Long[] ids){
        Boolean aBoolean = dispositionModuleService.deleteModuleById(Arrays.asList(ids));
        return aBoolean ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "多条件查询模块信息", notes = "多条件查询模块信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanDispositionModule", value = "JzJinzhuanDispositionModule", required = true, dataType = "JzJinzhuanDispositionModule"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/getModuleList", method = RequestMethod.POST)
    public Result<List<JzJinzhuanDispositionModule>> getModuleList(@RequestBody JzJinzhuanDispositionModule jzJinzhuanDispositionModule){
        if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){
            //竞赛一级管理员
            jzJinzhuanDispositionModule.setCreaterId(this.getUserId());
        }
        return Result.ok(dispositionModuleService.getModuleList(jzJinzhuanDispositionModule));
    }


    @ApiOperation(value = "分页多条件查询模块信息", notes = "分页多条件查询模块信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanDispositionModule", value = "JzJinzhuanDispositionModule", required = true, dataType = "JzJinzhuanDispositionModule"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/getPageModuleList", method = RequestMethod.POST)
    @RequiresPermissions("jzweb:disposition:getPageModuleList")
    public Result<Page<JzJinzhuanDispositionModule>> getPageModuleList(@RequestBody JzJinzhuanDispositionModule jzJinzhuanDispositionModule){
        if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){
            //竞赛一级管理员
            jzJinzhuanDispositionModule.setCreaterId(this.getUserId());
        }
        return Result.ok(dispositionModuleService.getPageModuleList(jzJinzhuanDispositionModule));
    }


    @ApiOperation(value = "获取考生成绩模版", notes = "获取考生成绩模版")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ModuleTopicMarkVo", value = "ModuleTopicMarkVo", required = true, dataType = "ModuleTopicMarkVo"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/selectModule", method = RequestMethod.POST)
    public Result selectModule(@RequestBody ModuleTopicMarkVo moduleTopicMarkVo){
        return dispositionModuleService.selectModule(moduleTopicMarkVo);
    }


    @ApiOperation(value = "获取任务模板信息", notes = "获取任务模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanModuleDetail", value = "JzJinzhuanModuleDetail", required = true, dataType = "JzJinzhuanModuleDetail"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/selectModuleDetail", method = RequestMethod.POST)
    public Result<?> selectModuleDetail(){
        Map map =new HashMap();
        if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){//不为空就不是超级管理员
            map.put("createrId",this.getUserId());
        }else if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("D")){
            map.put("encipher",this.getUserId());
        }
        List<JzJinzhuanModuleDetail> jzJinzhuanModuleDetails = dispositionModuleService.selectModuleDetail(map);
        return Result.ok(jzJinzhuanModuleDetails);
    }




}
